<?php defined('SYSPATH') or die('No direct script access.') ;
/**
 * 商品数据模型
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: item.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Model_Store_Item extends Model
{

    /**
     * 获取所有商品列表
     *
     * @return Database_Query_Builder_Select
     */
    public function all_sql()
    {
        return DB::select()
            ->from('store_items')
            ->where('deleted', '=', 0);
    }

    /**
     * 获取商品信息
     *
     * @param  int  $item_id
     * @return array
     */
    public function get($item_id)
    {
        return DB::select()
            ->from('store_items')
            ->where('deleted', '=', 0)
            ->where('item_id', '=', $item_id)
            ->fetch_row();
    }

    /**
     * 添加商品
     *
     * @param  array  $data
     * @return bool
     */
    public function add(array $data)
    {
        $v = $this->_validate($data);
        $this->validation($v);
        if ( ! $v->check() OR ! empty($this->_errors))
        {
            return FALSE;
        }

        $data = array(
            'title' => $v->title,
            'image_md5' => $v->image_md5,
            'spec' => $v->spec,
            'categories' => implode(',', (array) $v->categories),
            'attributes' => implode(',', (array) $v->attributes),
            'skins' => $v->skins,
            'summary' => $v->summary,
            'intro' => htmlspecialchars_decode($v->intro),
            'usage' => htmlspecialchars_decode($v->usage),
            'is_show' => (bool) $v->is_show,
        );
        $result = DB::insert('store_items', $data)->execute();

        return TRUE;
    }

    /**
     * 修改商品信息
     *
     * @param  int    $item_id
     * @param  array  $data
     * @return bool
     */
    public function edit($item_id, array $data)
    {
        $item = $this->get($item_id);
        if ( ! $item)
        {
            return $this->error('item_id', 'not_found');
        }

        $v = $this->_validate($data);
        $this->validation($v);

        if ( ! $v->check() OR ! empty($this->_errors))
        {
            return FALSE;
        }

        $data = array(
            'title' => $v->title,
            'image_md5' => $v->image_md5,
            'spec' => $v->spec,
            'categories' => implode(',', (array) $v->categories),
            'attributes' => implode(',', (array) $v->attributes),
            'skins' => $v->skins,
            'summary' => $v->summary,
            'intro' => htmlspecialchars_decode($v->intro),
            'usage' => htmlspecialchars_decode($v->usage),
            'is_show' => (bool) $v->is_show,
        );

        // 更新商品信息
        DB::update('store_items', $data)
            ->where('item_id', '=', $item_id)
            ->execute();

        return TRUE;
    }

    /**
     * 删除商品
     *
     * @param  int  $item_id
     * @return bool
     */
    public function del($item_id)
    {
        DB::update('store_items', array('deleted' => 1))
            ->where('item_id', '=', $item_id)
            ->execute();

        return TRUE;
    }

    /**
     * 数据验证
     *
     * @param  array  $data
     * @return Validation
     */
    protected function _validate(array $data)
    {
        return Validation::factory($data)
            ->rule('title', 'not_empty')
            ->rule('title', 'max_length', array(':value', 100))
            ->rule('categories', 'not_empty')
            ->rule('image_md5', 'not_empty')
            ->rule('summary', 'not_empty')
            ->rule('intro', 'not_empty');
    }

}